<?php

namespace api\controllers;

use common\core\actions\GetAction;
use common\core\actions\PostAction;
use server\UserInterface;
use yii\base\Module;

class UserController extends Controller
{

    public $user;

    public function __construct($id, Module $module, UserInterface $user, array $config = [])
    {
        $this->user = $user;
        parent::__construct($id, $module, $config);
    }

    public function actions()
    {
        return [
            'index' => [
                'class' => GetAction::class,
                'data' => function () {
                    //var_dump(\Yii::$app->user->getIdentity()->id);exit();
                    return $info = $this->user->getUserInfo();
                },
            ],
            'list' => [
                'class' => GetAction::class,
                'data' => function ($data) {
                    //var_dump(\Yii::$app->user->getIdentity()->id);exit();
                    return $this->getList($this->user->getUserList($data));

                },
            ],
            'add-user' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'username',
                    'gender',
                    'password_hash',
                    'roles',
                    'line_ids'
                ],
                'data' => function ($data) {
                    //unset($data['roles']);
                    //$data['roles'][] = ['name' => '超级管理员', 'type' => 1];
                    //var_dump($data);exit();
                    //$data['line_ids'] = implode(",", $data['line_ids']);
                    return $this->user->addUser($data, true);
                },
            ],
            'edit-user' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'id',
                    'gender',
                    'roles',
                    'line_ids',
                    'status'
                ],
                'data' => function ($data) {
                    return $this->user->editUser($data, true);
                },
            ],
            'reset-password' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'user_id',
                ],
                'data' => function ($data) {
                    $this->user->resetPassword($data['user_id']);
                    return [];
                },
            ],
            'change-password' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'password',
                    'repassword',
                    'newpassword',
                ],
                'data' => function ($data) {
                    return $this->user->changePassword($data);
                },
            ],
        ];
    }

}
